package com.wenjiang.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;


/**
 * mybatis 逆向工程
 */
public class CodeGenerator {
    public static void main(String[] args) {
        AutoGenerator autoGenerator =new AutoGenerator();

        // set freemarker engine
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());

        //创建数据源信息
        DataSourceConfig dataSourceConfig= new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/smallshop?serverTimezone=UTC&characterEncoding=utf8" +
                "&useUnicode=true&useSSL=false");

        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("123456");
        //装入数据源配置
        autoGenerator.setDataSource(dataSourceConfig);
        //全局配置
        GlobalConfig globalConfig= new GlobalConfig();
        //设置路径
        globalConfig.setOutputDir(System.getProperty("user.dir")+"/shop-service"+"/src/main/java");
        //生成后是否打开文件夹
        globalConfig.setOpen(false);
        //设置作者
        globalConfig.setAuthor("lwj");
        globalConfig.setServiceName("%sService");
        //装入全局配置
        autoGenerator.setGlobalConfig(globalConfig);


        //包信息
        PackageConfig packageConfig= new PackageConfig();
        //父包
        packageConfig.setParent("com.wenjiang");
        //在父包下创建子包把所有东西扔进去该包中
//        packageConfig.setModuleName("generator");
        packageConfig.setController("controller");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setMapper("mapper");
        packageConfig.setEntity("entity");
        autoGenerator.setPackageInfo(packageConfig);

        //配置策略
        StrategyConfig strategyConfig = new StrategyConfig();
        //开启lombook
        strategyConfig.setEntityLombokModel(true);
        //生成部分表 (表名）
        strategyConfig.setInclude("operation_log");
        //开启驼峰式命名
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        autoGenerator.setStrategy(strategyConfig);


        //生成
        autoGenerator.execute();


    }
}
